<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Detail_Proyek extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
		$this->load->library('session');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('proyek_model');
        $this->load->model('pegawai_model');
        $this->load->model('aktifitas_model');
		$this->load->model('evaluasi_model');
    }

    //load Grid
    function index($id_evaluasi) 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
			$colModel['NAMA_PROYEK'] = array('Nama Proyek', 200, TRUE, 'center', 1);
            $colModel['JENIS_PEKERJAAN'] = array('Jenis Pekerjaan', 150, TRUE, 'center', 1);
            $colModel['TANGGAL_MULAI_PROYEK'] = array('Tanggal Mulai Proyek', 100, TRUE, 'center', 1);
			$colModel['TANGGAL_SELESAI_PROYEK'] = array('Tanggal Selesai Proyek', 100, TRUE, 'center', 1);
			$colModel['NAMA_PEGAWAI'] = array('Nama Pegawai', 100, TRUE, 'center', 1);
			$colModel['STATUS_PROYEK'] = array('Status Proyek', 50, TRUE, 'center', 1);
			$colModel['Lihat_Daftar_Aktifitas'] = array('Lihat Daftar Aktifitas', 100, TRUE, 'center', 0);
			
            //get_evaluasi_name
			$hasil = $this->evaluasi_model->selectone($id_evaluasi);
			foreach ($hasil->result() as $row) {
                $nama_evaluasi = $row->NAMA_EVALUASI;
            }
			$this->session->set_userdata('ID_EVALUASI', $id_evaluasi);
			$this->session->set_userdata('NAMA_EVALUASI', $nama_evaluasi);

            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Detail Proyek</br>Evaluasi : '.$this->session->userdata('NAMA_EVALUASI').'',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Pilih Proyek', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke Daftar Evaluasi', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_foto
            $grid_js = build_grid_js('flex1', site_url("detail_proyek/grid_foto/".$id_evaluasi.""), $colModel, 'NAMA_PROYEK', 'desc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='Pilih Proyek')
                {
                        location.href='" . base_url() . "index.php/detail_proyek/pilih_proyek';
                }	
				
				if (com=='Kembali ke Daftar Evaluasi')
                {
                        location.href='" . base_url() . "index.php/evaluasi/index/';
                }

                if (com=='Hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' kursus?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/detail_proyek/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_foto($id_evaluasi) {
        $valid_fields = array('NAMA_PROYEK', 'JENIS_PEKERJAAN', 'TANGGAL_MULAI_PROYEK', 'TANGGAL_SELESAI_PROYEK', 'STATUS_PROYEK', 'NAMA_PEGAWAI');
        $this->flexigrid->validate_post('ID_PEGAWAI', 'desc', $valid_fields);
		$records = $this->proyek_model->get_data_proyek_pegawai($id_evaluasi);
        // $records = $this->proyek_model->get_data_aktif_proyek($id_evaluasi);
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) 
		{
			$status_aktif = "";
			$nama_proyek = str_replace("\'", "'", $row->NAMA_PROYEK);
            $record_items[] = array(
				
				$row->ID_PROYEK,
                $no = $no + 1,
                //'<a href= \'' . $lokasi_foto . '\' ' . $new_tab . '><img border=\'0\' src=\'' . $lokasi_foto . '\' height=\'50\'></a> ',
                $nama_proyek,
                $row->JENIS_PEKERJAAN,
                $row->TANGGAL_MULAI_PROYEK,
				$row->TANGGAL_SELESAI_PROYEK,
				$row->NAMA_PEGAWAI,
				$row->STATUS_PROYEK
				
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	//pilih kelas
    function pilih_proyek() 
	{
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['NAMA_PROYEK'] = array('Nama Proyek', 250, TRUE, 'center', 1);
            $colModel['JENIS_PEKERJAAN'] = array('Jenis Pekerjaan', 150, TRUE, 'center', 1);
            $colModel['TANGGAL_MULAI_PROYEK'] = array('Jadwal Proyek', 250, TRUE, 'center', 1);
			$colModel['TANGGAL_SELESAI_PROYEK'] = array('Jadwal Proyek', 250, TRUE, 'center', 1);
			$colModel['NAMA_PEGAWAI'] = array('Nama Pegawai', 150, TRUE, 'center', 1);
			$colModel['STATUS_PROYEK'] = array('Nama Pegawai', 150, TRUE, 'center', 1);

            $nama_evaluasi = $this->session->userdata('NAMA_EVALUASI');


            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
				'title' => 'Daftar Data Proyek',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambahkan proyek', 'add', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke detail proyek', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_pilih_proyek
            $grid_js = build_grid_js('flex1', site_url("detail_proyek/grid_pilih_proyek"), $colModel, 'NAMA_PROYEK', 'desc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
				if (com=='Kembali ke detail proyek')
                {
                        location.href='" . base_url() . "index.php/detail_proyek/index/".$this->session->userdata('ID_EVALUASI')."';
                }
				
                if (com=='Tambahkan proyek')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Tambahkan ' + $('.trSelected',grid).length + ' proyek?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/detail_proyek/add" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
														alert(data);
														window.location.href = '" . base_url() . "index.php/detail_proyek/index/".$this->session->userdata('ID_EVALUASI')."';
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid_pilih_proyek() {
        $valid_fields = array('NAMA_PROYEK', 'JENIS_PEKERJAAN', 'TANGGAL_MULAI_PROYEK', 'TANGGAL_SELESAI_PROYEK', 'STATUS_PROYEK', 'NAMA_PEGAWAI');
        $this->flexigrid->validate_post('NAMA_PROYEK', 'desc', $valid_fields);
        $records = $this->proyek_model->get_data_aktif_proyek();
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) {
		$nama_proyek = str_replace("\'", "'", $row->NAMA_PROYEK);
            $record_items[] = array(
                $row->ID_PROYEK,
                $no = $no + 1,
                $nama_proyek,
                $row->JENIS_PEKERJAAN,
				$row->TANGGAL_MULAI_PROYEK,
				$row->TANGGAL_SELESAI_PROYEK,
                $pegawai,
				$row->STATUS_PROYEK
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
		
	
    function add() {
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
			$id_evaluasi = $this->session->userdata('ID_EVALUASI');
            foreach ($spt_ids_post_array as $index => $id) 
			{
                if (isset($id) && $id != '') {
					$data = array(
                        'ID_PROYEK' => $id,
                        'ID_EVALUASI' => $this->session->userdata('ID_EVALUASI')
                    );
					
					//cek data yang sama
					$sql = $this->db->query("SELECT COUNT(id_aktifitas) AS CEK FROM aktifitas WHERE ID_PROYEK = ".$id." AND ID_EVALUASI = ".$id_evaluasi."");
					$cek = '';
					
					foreach ($sql->result() as $rows) {
						$cek = $rows->CEK;
					}
					
					$msg = '';
					if ($cek > 0) {
						$msg ='Data gagal ditambahkan. Data proyek sudah diambil.';
					}
					else{
						$this->aktifitas_model->insert($data);
						$msg ="Data proyek berhasil ditambahkan.";
					}
					$this->output->set_header($this->config->item('ajax_header'));
					$this->output->set_output($msg);
                }//end if
            }//end foreach
        }
    }
    
      
    public function form_nilai(){
		$data['status'] = 'new';
		$data['failed'] = false;
		$data['aa'] = '';
		$data['content'] = $this->load->view('form_nilai', $data, true);
		$this->load->view('main', $data);
	}
	
    function delete() {
        if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
  
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
                    $this->proyek_model->delete($id);
                }//end if
            }//end foreach
        }
    }

}
